﻿@{
    Layout = "~/Views/Shared/_LayoutPageAdmin.cshtml";
}
@model abc.Models.AdminLogin
<div id="page-wrapper">
    <div class="container-fluid">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h1 class="panel-title"> Edit Profile </h1>
                </div>
                <div class="panel-body">

                    <br />

                    <div class="row">
                        <div class="col-lg-2">
                            <label>
                                UserName
                            </label>
                        </div>
                        <div class="col-lg-3">
                            <label id="ShowUserName">
                                @Model.Admin_UserName
                            </label>
                            <button type="button" class="btn btn-primary" id="BtnEditUserName">
                                Edit
                            </button>
                        </div>
                        <div class="col-lg-7" id="DivEditUserName">
                            <div class="col-lg-5">
                                @Html.TextBoxFor(d => d.Admin_UserName, new { @class = "form-control", @id = "TextUserName", @placeholder = "Enter UserName" })
                            </div>
                            <div class="col-lg-4">
                                <input type="submit" class="btn btn-success" value="Save" id="BtnSaveUserName" />
                                <input type="button" class="btn btn-danger" value="Cancel" id="BtnCancelUserName" />
                            </div>
                        </div>
                    </div>

                    <br />

                    <div class="row">
                        <div class="col-lg-2">
                            <label>
                                Password
                            </label>
                        </div>
                        <div class="col-lg-3">
                            <button type="button" class="btn btn-primary" id="BtnEditPassword">
                                Edit Password
                            </button>
                        </div>
                        <div class="col-lg-7" id="DivEditPassword">
                            @Html.HiddenFor(d => d.Admin_Password, new { @id = "HiddenPasswordField" })
                            <div class="col-lg-5">
                                <input type="password" placeholder="Enter Old Password" class="form-control" id="TextOldPassword" />
                                <br />
                                <input type="password" placeholder="Enter New Password" class="form-control" id="TextNewPassword" />
                                <br />
                                <input type="password" placeholder="Re-Enter New Password" class="form-control" id="TextNewConfirmPassword" />
                            </div>
                            <div class="col-lg-2">
                                <input type="submit" class="btn btn-success" value="Save" id="BtnSavePassword" />
                                <br />
                                <br />
                                <input type="button" class="btn btn-danger" value="Cancel" id="BtnCancelPassword" />
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>

<script src="~/Scripts/jquery-2.1.3.min.js"></script>

<script>
    $(document).ready(function () {
        $("#DivEditUserName").hide();
        $("#DivEditPassword").hide();

        $("#BtnEditUserName").click(function () {
            $("#DivEditUserName").show();
        });

        $("#BtnEditPassword").click(function () {
            $("#DivEditPassword").show();
        });


        $("#BtnCancelUserName").click(function () {
            $("#DivEditUserName").hide();
        });

        $("#BtnCancelPassword").click(function () {
            $("#DivEditPassword").hide();
        });


        $("#BtnSaveUserName").click(function ()
        {
            var UserName = $("#TextUserName").val();
            $.post("/Admin/AdminEditUserName", { UserName: UserName }, function (data)
            {
                if (data == "Fail")
                {
                    alert("Couldn't Update UserName. Please try again !");
                }
                else
                {
                    alert("UserName Updated Successfully.");
                    $("#ShowUserName").html(data);
                    $("#TextUserName").val(data);
                }
            }, 'json');
            $("#DivEditUserName").hide();
        });

        $("#BtnSavePassword").click(function () {
            var Password = $("#TextOldPassword").val();
            var OldPassword = $("#HiddenPasswordField").val();
            var NewPassword = $("#TextNewPassword").val();
            var NewConfirmPassword = $("#TextNewConfirmPassword").val();
            if (Password == OldPassword) {
                if (NewPassword == NewConfirmPassword) {
                    $.post("/Admin/AdminEditPassword", { Password: NewPassword }, function (data) {
                        if (data == "Fail") {
                            alert("Couldn't Update Password. Please try again!");
                        }
                        else {
                            alert("Password Updated Successfully.");
                        }
                    }, 'json');
                    $("#HiddenPasswordField").val(NewPassword);
                    $("#TextOldPassword").val("");
                    $("#TextNewPassword").val("");
                    $("#TextNewConfirmPassword").val("");
                    $("#DivEditPassword").hide();
                }
                else {
                    alert("Passwords do not match!");
                    $("#TextOldPassword").val("");
                    $("#TextNewPassword").val("");
                    $("#TextNewConfirmPassword").val("");
                }
            }
            else {
                alert("Password is Incorrect!");
                $("#TextOldPassword").val("");
                $("#TextNewPassword").val("");
                $("#TextNewConfirmPassword").val("");
            }
        });
    });


</script>